/*
 * Twanoh Library, a useful Android utility library.
 * Copyright (C) 2006 Richard Schilling
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package gnu.org.twanoh.util;

import java.io.File;
import java.io.IOException;

import android.os.Environment;

/**
 * Utilities for working on the file system.
 * 
 * @author Richard Schilling (coderroadie@gmail.com)
 */
public final class FileSystemUtil {

    /**
     * Never instantiate.
     */
    private FileSystemUtil() {}

    /**
     * Checks to see if the specified directory is on a case sensitive file
     * system.
     * 
     * @return true if the file system is case sensitive.
     */
    public static boolean isCaseSensitive(File directory) throws IOException {

        if (!directory.isDirectory())
            throw new IOException("specified file must be a directory.");

        File f1, f2;
        int num = 1;

        /** find a file that does not exist. */
        do {
            f1 = new File(directory, "test." + num);
            f2 = new File(directory, "TEST." + num++);
        } while (f1.exists() || f2.exists());

        /* create the first file, then see if the second file is recognized. */
        try {

            if (!f1.createNewFile())
                throw new IOException(
                        "unable to create test file when checking for case sensitivity.");

            if (f2.exists())
                return false;

            if (f2.createNewFile())
                return true;
            else
                throw new IOException(
                        "unable to create test file when checking for case sensitivity.");

        } finally {
            f1.delete();
            f2.delete();
        }
    }

    public static boolean isExternalStorageAvailable() {
        boolean mExternalStorageAvailable = false;
        boolean mExternalStorageWriteable = false;
        String state = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            // We can read and write the media
            mExternalStorageAvailable = mExternalStorageWriteable = true;
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            // We can only read the media
            mExternalStorageAvailable = true;
            mExternalStorageWriteable = false;
        } else {
            // Something else is wrong. It may be one of many other states, but
            // all we need
            // to know is we can neither read nor write
            mExternalStorageAvailable = mExternalStorageWriteable = false;
        }

        return mExternalStorageAvailable && mExternalStorageWriteable ? true : false;

    }

}
